/**
 * @author Casanova
 */

function openCloseWordPanel()
{
	var current = $("#hdCurrentPanel")[0].value;
	var current = parseInt(current);
	if(current == -1 || current == 1)
	{
		$(".notePanel").fadeOut('fast', function() {
			$(".wordPanel").fadeIn('fast', function() {});
		});
		$("#hdCurrentPanel")[0].value = 0;
	}
	else
	{
		$(".wordPanel").fadeOut('fast', function() {} );
		$("#hdCurrentPanel")[0].value = -1;
	}
}

function openCloseNotePanel()
{
	var current = $("#hdCurrentPanel")[0].value;
	var current = parseInt(current);
	if (current == -1 || current == 0) 
	{
		$(".wordPanel").fadeOut('fast', function(){
			$(".notePanel").fadeIn('fast', function(){
			});
		});
		$("#hdCurrentPanel")[0].value = 1;
	}
	else
	{
		$(".notePanel").fadeOut('fast', function() {} );
		$("#hdCurrentPanel")[0].value = -1;
	}
}

function ChooseTest(value)
{
	var url = '../Ajax/TestProcess.php?type=1&id=' + value;
	$.getJSON(url, function(data){
		$("#tbDes")[0].value = data.des;
	});
}

function ChangeQuestion(input, num)
{
	SendUserAnswers();
	currentIndex = parseInt($("#hdCurrentIndex")[0].value) + parseInt(num);
	var url = '../Ajax/UserControlProcess.php?type=0&id=' + input.title + '&index=' + currentIndex;
	$.getJSON(url, function(data){
		if (data.result == true) {
			if (data.Paragraph == "") 
				$(".paragraphArea")[0].innerHTML = "";
			else 
				$(".paragraphArea")[0].innerHTML = data.Paragraph.Content + GetMediaPlayer(data.Paragraph.Sound);
			$(".questionArea")[0].innerHTML = (currentIndex + 1) + ". " + data.Question.Content + GetMediaPlayer(data.Question.Sound);
			$("#hdCurrentIndex")[0].value = currentIndex;
			$("#hdCurrentQuestion")[0].value = data.Question.Id;
			if(data.prev == -1)
				$("#btPrev")[0].disabled = true;
			else {
				$("#btPrev")[0].disabled = false;
				$("#btPrev")[0].title = data.prev;
			}
			if(data.next == -1)
				$("#btNext")[0].disabled = true;
			else {
				$("#btNext")[0].disabled = false;
				$("#btNext")[0].title = data.next;
			}
			answerStr = "";
			answerType = data.MultiChoice == true ? "checkbox" : "radio";
			for(as = 0; as < data.Answers.length; as++)
			{
				answerItem = data.Answers[as];
				check = "";
				if(answerItem.IsChecked == true)
					check = "checked";
				answerStr = answerStr + "<input " + check + " type='" + answerType + "' name='inputAnswers' value='" + answerItem.Id + "'>" + answerItem.Content + "</input><br/>";
			}
			$(".answerArea")[0].innerHTML = answerStr;
			$("#hdRights")[0].value = data.AnswerRight;
		}
	});
}

function SendUserAnswers()
{
	index = parseInt($("#hdCurrentIndex")[0].value);
	var url = '../Ajax/UserControlProcess.php?type=1&question=' + $("#hdCurrentQuestion")[0].value + '&index=' + index + '&answers=';
	answers = document.getElementsByName("inputAnswers");
	answerIds = "";
	for(as = 0 ; as < answers.length; as++)
	{
		if(answers[as].checked)
		{
			answerIds = answerIds + answers[as].value + "_";
		}
	}
	if(answerIds != "")
		answerIds = answerIds.substring(0, answerIds.length - 1);
	url = url + answerIds;
	$.getJSON(url, function(data){
		
	});
}

function FinishTest()
{
	if(confirm("Are you sure to complete your test ?") == true)
	{
		SendUserAnswers();
		window.location = "../FE/FinishTest.php";
	}
}

function ShowPanel(type)
{
	if(type == 1)
	{
		$("#panelContent").css('display', 'inline');
		$("#panelContentNote").css('display', 'none');
		$("#panelContentWord").css('display', 'none');
	}
	else if(type == 2)
	{
		$("#panelContent").css('display', 'none');
		$("#panelContentNote").css('display', 'inline');
		$("#panelContentWord").css('display', 'none');
	}
	else if(type == 3)
	{
		$("#panelContent").css('display', 'none');
		$("#panelContentNote").css('display', 'none');
		$("#panelContentWord").css('display', 'inline');
	}
	var winl = (screen.width-430)/2;
		$.blockUI({
			message: $('div.popupPanel'),
			css: {
				width: '430px',
				height: '270px',
				cursor: 'pointer',	
				'text-align':'left',
				top:"15%", 
				left:winl,
				position:'fixed',
				'border-color':'white',
				'background-image':'url(../images/popupPanel_bg.jpg)'
			}
		}); 
}

function SendFeedback()
{
	des = $("#tbFeedback")[0].value;
	id = $("#hdCurrentQuestion")[0].value;
	if(des == "")
	{
		alert("You must input description for this feedback");
		return;
	}
	$.getJSON('../Ajax/UserControlProcess.php?type=3',{ 'des':des, 'id':id }, function(data) {
		if(data.result == true)
		{
			$("#tbFeedback")[0].value = "";
			ClosePopupPanel();
		}
		else
		{
			alert("Cannot create feedback");
			$("#tbFeedback")[0].value = "";
			ClosePopupPanel();
		}
	});
}

function SendNote()
{
	note = $("#tbNote")[0].value;
	if(note == "")
	{
		alert("You must input content for this note");
		return;
	}
	$.getJSON('../Ajax/UserControlProcess.php?type=4',{ 'note':note }, function(data) {
		if(data.result == true)
		{
			$("#tbNote")[0].value = "";
			ClosePopupPanel();
		}
		else
		{
			alert("Cannot create note");
			$("#tbNote")[0].value = "";
			ClosePopupPanel();
		}
	});
}

function SendWord()
{
	key = $("#tbAddKey")[0].value;
	trans = $("#tbTranslate")[0].value;
	if(key == "" || trans == "")
	{
		alert("You must input key and translate of word!");
		return;
	}
	type = $("#rbEV")[0].value;
	if($("#rbVE")[0].checked == true)
		type = $("#rbVE")[0].value;
	$.getJSON('../Ajax/UserControlProcess.php?type=6',{ 'key':key, 'trans':trans, 'typeWord':type }, function(data) {
		if(data.result == true)
		{
			$("#tbAddKey")[0].value = "";
			$("#tbTranslate")[0].value = "";
			$("#rbEV")[0].checked = true
			ClosePopupPanel();
		}
		else
		{
			alert("Cannot add word because the key is available");
		}
	});
}

function GetWord()
{
	key = $("#tbKey")[0].value;
	if(key == "")
	{
		alert("You must input key of word!");
		return;
	}
	type = $("#rbViewEV")[0].value;
	if($("#rbViewVE")[0].checked == true)
		type = $("#rbViewVE")[0].value;
	$.getJSON('../Ajax/UserControlProcess.php?type=7',{ 'key':key, 'typeWord':type }, function(data) {
		$("#tbViewWord")[0].value = data.result;
	});
}

function GetNote(num)
{
	index = parseInt($("#hdCurrentIndexNote")[0].value) + parseInt(num);
	$.getJSON('../Ajax/UserControlProcess.php?type=5&index='+ index, function(data) {
		if(data.result == true)
		{
			$("#tbViewNote")[0].value = data.content;
			$("#hdCurrentIndexNote")[0].value = index;
		}
	});
}

function ShowDescription(str)
{
	var winl = (screen.width-430)/2;
	$("#panelContent")[0].innerHTML = str;
	$('div.flash').css('visibility', 'hidden');
	$.blockUI({
		message: $('div.popupPanel'),
		css: {
			width: '430px',
			height: '270px',
			cursor: 'pointer',	
			'text-align':'left',
			top:"15%", 
			left:winl,
			position:'fixed',
			'border-color':'white',
			'background-image':'url(../images/popupPanel_bg.jpg)'
		}
	}); 
}

function SearchFeedback()
{
	status = $("#cbStatus")[0].value;
	url = document.forms[0].getAttribute("action");
	array = url.split("&");
	document.forms[0].setAttribute("action", array[0] + "&status=" + status);
	document.forms[0].submit();
}

function SetSelectedValue()
{
	strId = "";
	selected = $(".checkItems");
	for(ss = 0; ss < selected.length; ss++)
	{
		if(selected[ss].checked == true)
		{
			strId = strId + selected[ss].value + ",";
		}
	}
	if(strId != "")
	{
		strId = strId.substring(0, strId.length - 1);
	}
	$("#hdSelected")[0].value = strId;
}

function ChangeStatusFeedback()
{
	SetSelectedValue();
	$("#hdCommand")[0].value = 0;
	document.forms[0].submit();
}

function DeleteItems(itemName)
{
	if(confirm("Are you sure to delete seletec " + itemName + " ?") == true)
	{
		SetSelectedValue();
		$("#hdCommand")[0].value = 1;
		document.forms[0].submit();
	}
}

function viewParagraphContent(id)
{
	var url = '../Ajax/QuestionProcess.php?type=6&id=' + id;
	$.getJSON(url, function(data) {
		$("#panelContent")[0].innerHTML = data.result;
		var winl = (screen.width-720)/2;
		$('div.flash').css('visibility', 'hidden');
		$.blockUI({
			message: $('div.popupPanel'),
			css: {
				width: '720px',
				height: '420px',
				cursor: 'pointer',	
				'text-align':'left',
				top:"15%", 
				left:winl,
				position:'fixed',
				'border-color':'white',
				'background-image':'url(../images/popupPanel_bg.jpg)'
			}
		}); 
	});
}

function ViewAnswers()
{
	str = $("#hdRights")[0].value;
	ids = str.split("_");
	answers = document.getElementsByName("inputAnswers");
	for(as = 0 ; as < answers.length; as++)
	{
		answers[as].checked = false;
	}
	for(r = 0 ; r < ids.length ; r++)
	{
		for(as = 0 ; as < answers.length; as++)
		{
			if(answers[as].value == ids[r])
			{
				answers[as].checked = true;
				break;
			}
		}
	}
}

function CountDown(min, sec)
{
	if(sec > 0)
	{
		sec--;
		$(".timer")[0].innerHTML = min + ":" + sec;
		setTimeout("CountDown(" + min +  "," + sec + ")", 1000);
	}
	else
	{
		if(min > 0)
		{
			if(min == 1)
			{
				$(".timer").css({
					color: 'red',
					'text-decoration': 'blink'
				});
			}
			min--;
			sec = 59;
			$(".timer")[0].innerHTML = min + ":" + sec;
			setTimeout("CountDown(" + min +  "," + sec + ")", 1000);
		}
		else
		{
			SendUserAnswers();
			window.location = "../FE/FinishTest.php";
		}
	}
}

function SubmitLogin()
{
	kq = true;
	if($("#tbUsername")[0].value == "")
	{
		$("#tbUsername").css('background-color', 'red');
		kq = false;
	}
	else
	{
		$("#tbUsername").css('background-color', 'white');
	}
	if($("#tbPassword")[0].value == "")
	{
		$("#tbPassword").css('background-color', 'red');
		kq = false;
	}
	else
	{
		$("#tbPassword").css('background-color', 'white');
	}
	if(kq == true)
	{
		document.forms[0].submit();
	}
}

function mouseHover(img)
{
	switch(img.title)
	{
		case "../images/1-b1.gif":
		case "../images/2-b2.gif":
		case "../images/3-b3.gif":
		case "../images/4-b4.gif":
		case "../images/5-b5.gif":
		case "../images/6-b6.gif":
			break;
		case "../images/1-b2.gif":
			img.title = img.src = "../images/2-b2.gif";
			break;
		case "../images/1-b3.gif":
			img.title = img.src = "../images/3-b3.gif"
			break;
		case "../images/1-b4.gif":
			img.title = img.src = "../images/4-b4.gif";
			break;
		case "../images/1-b5.gif":
			img.title = img.src = "../images/5-b5.gif"
			break;
		case "../images/1-b6.gif":
			img.title = img.src = "../images/6-b6.gif";
			break;
		case "../images/2-b1.gif":
			img.title = img.src = "../images/1-b1.gif"
			break;
	}
}

function mouseOut(img)
{
	switch(img.title)
	{
		case "../images/1-b2.gif":
		case "../images/1-b3.gif":
		case "../images/1-b4.gif":
		case "../images/1-b5.gif":
		case "../images/1-b6.gif":
		case "../images/2-b1.gif":
		case $("#currentButton")[0].value:
			break;
		case "../images/1-b1.gif":
			img.title = img.src = "../images/2-b1.gif";
			break;
		case "../images/2-b2.gif":
			img.title = img.src = "../images/1-b2.gif";
			break;
		case "../images/3-b3.gif":
			img.title = img.src = "../images/1-b3.gif";
			break;
		case "../images/4-b4.gif":
			img.title = img.src = "../images/1-b4.gif";
			break;
		case "../images/5-b5.gif":
			img.title = img.src = "../images/1-b5.gif";
			break;
		case "../images/6-b6.gif":
			img.title = img.src = "../images/1-b6.gif";
			break;
	}
}

function GetMediaPlayer(url)
{
	if(url == null || url == "")
		return "";
	strPlayer = "<object id='myWindowMedia' width='300' height='45' name='winMediaPlayer' classid='CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject' codebase='http://activex.microsof.../mplayer/en/nsmp2inf.cab#Version=6,4,5,715'>"
					+ "<param value='" + url + "' name='URL'/>"
					+ "<param value='1' name='rate'/>"
					+ "<param value='0' name='balance'/>"
					+ "<param value='0' name='currentPosition'/>"
					+ "<param value='0' name='defaultFrame'/>"
					+ "<param value='999' name='playCount'/>"
					+ "<param value='-1' name='CursorType'/>"
					+ "<param value='1' name='autoStart'/>"
					+ "<param value='0' name='currentMarker'/>"
					+ "<param value='-1' name='invokeURLs'/>"
					+ "<param value='50' name='volume'/>"
					+ "<param value='0' name='mute'/>"
					+ "<param value='-1' name='stretchToFit'/>"
					+ "<param value='0' name='windowlessVideo'/>"
					+ "<param value='1' name='enabled'/>"
					+ "<param value='0' name='fullScreen'/>"
					+ "<param value='0' name='enableContextMenu'/>"
					+ "<param value='0' name='enableErrorDialogs'/>"
					+ "<embed id='embedMedia' width='300' height='45' showtracker='1' showstatusbar='1' showpositioncontrols='1' showgotobar='0' showdisplay='0' showaudiocontrols='1' showcontrols='1' playcount='999' mute='0' enabletracker='1' enablefullscreencontrols='1' enablecontextmenu='0' displaysize='1' clicktoplay='1' autostart='0' autosize='1' name='MediaPlayerTV' src='" + url + "' pluginspage='http://www.microsoft.co...s/windowsmedia/download/' type='application/x-mplayer2'/>"
				+ "</object>";
	return strPlayer;
}
